#include "camprec.h"
#include "MgrExtModules.h"

#include <iostream>

//#include "MYHEADER.h" // dlltest

HINSTANCE CMgrExtModules::hMGREXT = 0;

EXT::IMGR_EXT CMgrExtModules::IMGREXT = 0;

const wchar_t* CMgrExtModules::MGREXT_NAME_DLL  = L"MgrExt.dll";

CMgrExtModules::CMgrExtModules(void)
{
}


CMgrExtModules::~CMgrExtModules(void)
{
}


bool CMgrExtModules::InitMGR_EXT ()
{
	if (hMGREXT != 0)
		return true;

	hMGREXT = ::LoadLibrary(MGREXT_NAME_DLL);

	if (!hMGREXT)
	{
		std::cout << "\nERROR loading Manager extension library (library not found)\n";
		return false;
	}
	
		EXT::INTERFACE_MGR_EXT pfnInterface = (EXT::INTERFACE_MGR_EXT) ::GetProcAddress(hMGREXT,  "INITMGR");

		if (!pfnInterface)
		{
			::FreeLibrary(hMGREXT);
			hMGREXT = 0;

			std::cout << "\nERROR loading Manager extension library (interface not found)\n";

			return false;
		}
		
		pfnInterface(&IMGREXT);

		if (IMGREXT == 0)
		{
			::FreeLibrary(hMGREXT);
			hMGREXT = 0;

			std::cout << "\nERROR loading Manager extension library (interface cannot by init)\n";
			
			return false;
		}

		// dlltest
		//MyClass x;
		//x.ss = x.ss + L"123";
		//x.VectorOfInts.push_back(1);
		//dll_wstring ss;
		//IMGREXT->SetText (ss);
		return true;
};

void CMgrExtModules::FreeMGR_EXT ()
{
	if (IMGREXT != 0)
		IMGREXT->Release();
	::FreeLibrary (hMGREXT);
	std::cout << "\nUnloading Manager extension library...\n";
};